package vns.client;

import java.util.ArrayList;
import java.util.List;

import vns.client.event.ObjectPropertyTypeEvent;
import vns.client.event.ObjectTypeEvent;
import vns.client.event.RoleEvent;
import vns.client.event.RuleSetEvent;
import vns.client.helper.Helper;
import vns.client.presenter.BusyIndicatorPresenter;
import vns.client.view.BusyIndicatorView;
import vns.shared.AccountData;
import vns.shared.LoginInfo;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class VNSmartUI implements EntryPoint {

	private RootPanel rootPanel;
	private static VNSmartUI singleton;
	private SimpleEventBus eventBus = new SimpleEventBus();
	private AppController appViewer = new AppController(eventBus);
	private Helper helper = new Helper();
	private List<Button> listButtonOtype = new ArrayList<Button>();
	private HTMLPanel appPanel;
	private AccountData accountData = null;
	private DialogBox dialogBox = new DialogBox();
	private DialogBox dialogBoxSub = new DialogBox();

	@SuppressWarnings("unused")
	private BusyIndicatorPresenter busyIndicator = new BusyIndicatorPresenter(
			eventBus, new BusyIndicatorView("Đang chạy chương trình ..."));

	/*
	 * UIBinder
	 */
	private static VNSmartUIUiBinder uiBinder = GWT
			.create(VNSmartUIUiBinder.class);
	@UiField
	Label labelVNSHeader;
	@UiField
	MenuBar menuVNSMainMenu;
	@UiField
	HTMLPanel htmlpanelVNSMainPanel;
	@UiField
	HTMLPanel htmlpanelVNSChildPanel;
	@UiField
	StackPanel stackPanelVNSLeftMenu;
	@UiField
	Label labelVNSFooter;
	@UiField
	MenuItem menuItemQuanLyKieuDuLieu;
	@UiField
	MenuItem menuItemQuanLyKieuDanhSach;
	@UiField
	MenuItem menuItemQuyenhan;
	@UiField
	MenuItem menuItemQuyTrinh;
	@UiField
	Label labelBreadcrum;
	@UiField
	HTML htmlAccount;
	@UiField
	Label labelChildBreadcrum;
	@UiField
	VerticalPanel vpanelList;
	@UiField
	VerticalPanel vpanelStat;
	@UiField
	VerticalPanel vpanelRelated;
	@UiField
	VerticalPanel vpanelPickList;
	@UiField
	Button btBack;
	@UiField
	Button btForward;

	interface VNSmartUIUiBinder extends UiBinder<HTMLPanel, VNSmartUI> {
	}

	@Override
	public void onModuleLoad() {
		singleton = this;
		appPanel = uiBinder.createAndBindUi(this);
		rootPanel = RootPanel.get();
		appViewer.go();
		init();
		bind();
	}

	private void init() {
		dialogBox.setGlassEnabled(true);
		dialogBox.setAnimationEnabled(true);
		dialogBoxSub.setGlassEnabled(true);
		dialogBoxSub.setAnimationEnabled(true);
		
		Button bt = new Button("Tắt");
		bt.setWidth("100%");
		bt.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				VNSmartUI.get().getDialogBoxSub().hide();
			}
		});
		dialogBoxSub.add(bt);
	}

	private void bind() {
		menuItemQuanLyKieuDuLieu.setCommand(new Command() {
			@Override
			public void execute() {
				eventBus.fireEvent(new ObjectPropertyTypeEvent());
			}
		});
		menuItemQuanLyKieuDanhSach.setCommand(new Command() {
			@Override
			public void execute() {
				eventBus.fireEvent(new ObjectTypeEvent());
			}
		});
		menuItemQuyenhan.setCommand(new Command() {
			@Override
			public void execute() {
				eventBus.fireEvent(new RoleEvent());
			}
		});
		menuItemQuyTrinh.setCommand(new Command() {
			@Override
			public void execute() {
				eventBus.fireEvent(new RuleSetEvent());
			}
		});
	}

	/****************************************************/
	@UiHandler("btBack")
	void onBtBackClick(ClickEvent event) {
		back();
	}

	@UiHandler("btForward")
	void onBtForwardClick(ClickEvent event) {
		forward();
	}

	private void loadAppPanel() {
		rootPanel.clear();
		rootPanel.add(appPanel);
	}

	/****************************************************/
	
	public boolean isUserRegistered(String email){
		return true;
	}

	public void signIn() {
//		this.htmlpanelVNSMainPanel.setVisible(true);
//		this.htmlpanelVNSChildPanel.setVisible(true);
//		this.menuVNSMainMenu.setVisible(true);
//		this.stackPanelVNSLeftMenu.setVisible(true);
		loadAppPanel();
	}

	public void signOut() {
		// this.htmlpanelVNSMainPanel.setVisible(false);
//		this.htmlpanelVNSChildPanel.setVisible(false);
//		this.menuVNSMainMenu.setVisible(false);
//		this.stackPanelVNSLeftMenu.setVisible(false);
	}

	public void setToken(String s){
		appViewer.setToken(s);
	}

	public void callLastToken(){
		appViewer.callLastToken();
	}
	
	public void back() {
		appViewer.back();
	}

	public void forward() {
		appViewer.forward();
	}

	public static VNSmartUI get() {
		return singleton;
	}

	public void reloadVpanelList() {
		appViewer.loadVpanelList();
	}

	public Label getLabelBreadcrum() {
		return labelBreadcrum;
	}

	void setLabelBreadcrum(String breadcrum) {
		this.labelBreadcrum.setText(breadcrum);
		this.labelBreadcrum.setStyleName("vnsBreadcrum");
	}

	public Label getLabelChildBreadcrum() {
		return labelChildBreadcrum;
	}

	void setLabelChildBreadcrum(String breadcrumChild) {
		this.labelChildBreadcrum.setText(breadcrumChild);
		this.labelChildBreadcrum.setStyleName("vnsBreadcrum");
	}

	HTMLPanel getMainPanel() {
		return htmlpanelVNSMainPanel;
	}

	HTMLPanel getChildPanel() {
		return htmlpanelVNSChildPanel;
	}

	public SimpleEventBus getEventBus() {
		return eventBus;
	}

	public VerticalPanel getVpanelList() {
		return vpanelList;
	}

	public VerticalPanel getVpanelStat() {
		return vpanelStat;
	}

	public VerticalPanel getVpanelRelated() {
		return vpanelRelated;
	}

	public VerticalPanel getVpanelPickList() {
		return vpanelPickList;
	}

	public void setListButtonOtype(List<Button> listButtonOtype) {
		this.listButtonOtype = listButtonOtype;
	}

	public List<Button> getListButtonOtype() {
		return listButtonOtype;
	}

	public HTML getHtmlAccount() {
		return htmlAccount;
	}

	public void setHtmlAccount(String html) {
		this.htmlAccount.setHTML(html);
	}

	public void setAccountData(AccountData accountData) {
		this.accountData = accountData;
	}

	public AccountData getAccountData() {
		return accountData;
	}

	public void setDialogBox(DialogBox dialogBox) {
		this.dialogBox = dialogBox;
	}

	public DialogBox getDialogBox() {
		return dialogBox;
	}

	public void setDialogBoxSub(DialogBox dialogBoxSub) {
		this.dialogBoxSub = dialogBoxSub;
	}

	public DialogBox getDialogBoxSub() {
		return dialogBoxSub;
	}

}